package com.ruoyi.web.controller.finance;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.finance.domain.KltInvoicingDetail;
import com.ruoyi.finance.domain.KltStock;
import com.ruoyi.finance.domain.VO.KltInvoicingDetailVO;
import com.ruoyi.finance.service.KltInvoicingDetailService;
import com.ruoyi.finance.service.KltStockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * (KltInvoicingDetail)表控制层
 *
 * @author makejava
 * @since 2025-02-13 15:09:04
 */
@RestController
@RequestMapping("finance/kltInvoicingDetail")
public class KltInvoicingDetailController  {
    /**
     * 服务对象
     */
    @Resource
    private KltInvoicingDetailService kltInvoicingDetailService;
    @Autowired
    private KltStockService kltStockService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param kltInvoicingDetail 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    public AjaxResult selectAll(Page<KltInvoicingDetail> page, KltInvoicingDetail kltInvoicingDetail) {
        Page<KltInvoicingDetail> page1 = this.kltInvoicingDetailService.page(page, new QueryWrapper<>(kltInvoicingDetail));
        List<KltInvoicingDetailVO> kltInvoicingDetailVOS = new ArrayList<>();

        page1.getRecords().forEach(kltInvoicingDetail1 -> {
            KltInvoicingDetailVO kltInvoicingDetailVO = new KltInvoicingDetailVO();

            KltStock kltStock = kltStockService.getById(kltInvoicingDetail1.getStockId());
            kltInvoicingDetailVO.setProductName(kltStock.getProductName());
            kltInvoicingDetailVO.setModel(kltStock.getModel());
            kltInvoicingDetailVO.setAmount(kltInvoicingDetail1.getAmount());
            kltInvoicingDetailVO.setRate(kltInvoicingDetail1.getRate());
            kltInvoicingDetailVO.setPrice(kltInvoicingDetail1.getPrice());
            kltInvoicingDetailVO.setInvoicingDetailAmount(kltInvoicingDetail1.getInvoicingDetailAmount());
            kltInvoicingDetailVOS.add(kltInvoicingDetailVO);
        });
        return success(kltInvoicingDetailVOS);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {

        return success(this.kltInvoicingDetailService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param kltInvoicingDetail 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody KltInvoicingDetail kltInvoicingDetail) {
        return success(this.kltInvoicingDetailService.save(kltInvoicingDetail));
    }

    /**
     * 修改数据
     *
     * @param kltInvoicingDetail 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody KltInvoicingDetail kltInvoicingDetail) {
        return success(this.kltInvoicingDetailService.updateById(kltInvoicingDetail));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(this.kltInvoicingDetailService.removeByIds(idList));
    }
}

